package per.dg.basicuseexecutor

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import java.util.concurrent.*

class MainActivity : AppCompatActivity() {

    private val TAG = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    private fun creatFixedThreadPool() : ThreadPoolExecutor{
        val corePoolSize : Int = 2
        return Executors.newFixedThreadPool(corePoolSize) as ThreadPoolExecutor
    }

    private fun useFixedThreadPool(threadPoolExecutor : ThreadPoolExecutor){
        for(i in 0..10){
            val runnable = object : Runnable {
                override fun run() {
                    try {
                        Thread.sleep(2000)
                        Log.e(TAG, "当前线程名称: " + Thread.currentThread().name )
                    }catch (e : InterruptedException){

                    }
                }
            }
            threadPoolExecutor.execute(runnable)
        }
    }

    private fun creatSingleThreadExecutor() : ExecutorService{
        return Executors.newSingleThreadExecutor() as ExecutorService
    }

    private fun useSingleThreadExecutor(executorService : ExecutorService){
        for(i in 0..10){
            val runnable = object : Runnable {
                override fun run() {
                    try {
                        Thread.sleep(2000)
                        Log.e(TAG, "当前线程名称: " + Thread.currentThread().name )
                    }catch (e : InterruptedException){

                    }
                }
            }
            executorService.execute(runnable)
        }
    }

    private fun creatCachedThreadPool(): ThreadPoolExecutor {
        return Executors.newCachedThreadPool() as ThreadPoolExecutor
    }

    private fun useCachedThreadPool(threadPoolExecutor: ThreadPoolExecutor){
        for(i in 0..10){
            val runnable = object : Runnable {
                override fun run() {
                    try {
                        Thread.sleep(2000)
                        Log.e(TAG, "当前线程名称: " + Thread.currentThread().name )
                    }catch (e : InterruptedException){

                    }
                }
            }
            threadPoolExecutor.execute(runnable)
        }
    }

    private fun creatScheduledThreadPool(): ScheduledExecutorService {
        return Executors.newScheduledThreadPool(5)
    }

    private fun useScheduledThreadPool(scheduledExecutorService: ScheduledExecutorService){
        for(i in 0..10){
            val runnable = object : Runnable {
                override fun run() {
                    try {
                        Thread.sleep(2000)
                        Log.e(TAG, "当前线程名称: " + Thread.currentThread().name )
                    }catch (e : InterruptedException){

                    }
                }
            }
            scheduledExecutorService.schedule(runnable, 5, TimeUnit.SECONDS)
        }
    }

    private fun creatSingleThreadScheduledExecutor() : ScheduledExecutorService{
        return Executors.newSingleThreadScheduledExecutor() as ScheduledExecutorService
    }

    private fun useSingleThreadScheduledExecutor(scheduledExecutorService: ScheduledExecutorService){
//        for(i in 0..10){
            val runnable = object : Runnable {
                override fun run() {
                    try {
                        Thread.sleep(2000)
                        Log.e(TAG, "当前线程名称: " + Thread.currentThread().name )
                    }catch (e : InterruptedException){

                    }
                }
            }
            scheduledExecutorService.scheduleAtFixedRate(runnable, 2, 5, TimeUnit.SECONDS)
//        }
    }

    fun onClick(view: View) {
        when(view.id){
            R.id.bt_creat_and_start_fixed_thread_pool ->{
                useFixedThreadPool(creatFixedThreadPool())
            }

            R.id.bt_creat_and_start_single_thread_executor ->{
                useSingleThreadExecutor(creatSingleThreadExecutor())
            }

            R.id.bt_creat_and_start_cached_thread_pool ->{
                useCachedThreadPool(creatCachedThreadPool())
            }

            R.id.bt_creat_and_start_scheduled_thread_pool ->{
                useScheduledThreadPool(creatScheduledThreadPool())
            }

            R.id.bt_creat_and_start_single_thread_scheduled_executor ->{
                useSingleThreadScheduledExecutor(creatSingleThreadScheduledExecutor())
            }
        }
    }
}